package com.smarthealthring.mm.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.contract.UnloginContract;
import com.smarthealthring.mm.event.FinishHomeEvent;
import com.smarthealthring.mm.presenter.UnloginPresenter;
import com.smarthealthring.mm.utils.ComToolBar;
import com.smarthealthring.mm.utils.FileCacheUtils;
import com.smarthealthring.mm.utils.RandomUtil;
import com.smarthealthring.mm.utils.RxBus;
import com.smarthealthring.mm.utils.SPUtils;
import com.smarthealthring.mm.utils.ToastUtil;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;

import static com.smarthealthring.mm.utils.SPUtils.USERNAME;

/**
 * 设备类型列表
 * Created by bilibili on 2018/5/15.
 */

public class SettingActivity extends BaseActivity<UnloginPresenter> implements UnloginContract.View {

    @BindView(R.id.btn_unlogin)
    Button mBtnUnlogin;
    @BindView(R.id.rl_unit_set)
    RelativeLayout mRlUnitSet;
    @BindView(R.id.rl_msg_set)
    RelativeLayout mRlMsgSet;
    @BindView(R.id.rl_clean_cash)
    RelativeLayout mRlCleanCash;
    @BindView(R.id.rl_feed_back)
    RelativeLayout mRlFeedBack;
    @BindView(R.id.rl_about)
    RelativeLayout mRlAbout;
    @BindView(R.id.tv_cache_size)
    TextView mTvCacheSize;

    @Override
    protected void initComToolBar() {
        ImageView ivLeft = ComToolBar.setleftImg(mContext, R.drawable.back_white);
        RxView.clicks(ivLeft)
                .subscribe(o -> finish());
        ComToolBar.setTitle(mContext, "设置");
    }

    @Override
    protected int getLayout() {
        return R.layout.act_setting;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {

        // 退出登录
        mBtnUnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.unlogin(new HashMap<String, String>() {{
                    put("trans_id", "2008");
                    put("nonce_str", RandomUtil.getRandomString(32));
                    put("username", SPUtils.getString(USERNAME, ""));
                    put("token", SPUtils.getString(SPUtils.TOKEN, "token=="));
                    put("sign", "1111");
                }});
            }
        });

        // 单位设置
        mRlUnitSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, UnitSetActivity.class));
            }
        });

        // 消息设置
        mRlMsgSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, MsgSetActivity.class));
            }
        });

        // 意见反馈
        mRlFeedBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(SettingActivity.this,UnitSetActivity.class));
            }
        });

        // 关于
        mRlAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
            }
        });

        // 清理缓存
        mRlCleanCash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.longShow("正在清理");
//                FileCacheUtils.cleanInternalCache(mContext);
//                FileCacheUtils.cleanExternalCache(mContext);
                FileCacheUtils.clearAllCache(mContext);
//                setDefaultSize();
                initCacheSize();
            }
        });

        initCacheSize();

    }

    @Override
    protected UnloginPresenter createPresenter() {
        return new UnloginPresenter();
    }

    @Override
    public void unloginSuccess() {
        Log.e("注销成功回调===", "in...");
        SPUtils.setString(USERNAME, "");
        startActivity(new Intent(this, LoginActivity.class));

        RxBus.getInstance().send(new FinishHomeEvent());
        finish();

    }

    @Override
    public void unloginFail() {

    }

    private void initCacheSize() {
    /*
    * 获取SD卡根目录：Environment.getExternalStorageDirectory().getAbsolutePath();
        外部Cache路径：/mnt/sdcard/android/data/com.xxx.xxx/cache 一般存储缓存数据（注：通过getExternalCacheDir()获取）
        外部File路径：/mnt/sdcard/android/data/com.xxx.xxx/files 存储长时间存在的数据
        （注：通过getExternalFilesDir(String type)获取， type为特定类型，可以是以下任何一种
                    Environment.DIRECTORY_MUSIC,
                    Environment.DIRECTORY_PODCASTS,
                     Environment.DIRECTORY_RINGTONES,
                     Environment.DIRECTORY_ALARMS,
                     Environment.DIRECTORY_NOTIFICATIONS,
                     Environment.DIRECTORY_PICTURES,
                      Environment.DIRECTORY_MOVIES. ）
    * */
        String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File outCachePath = getExternalCacheDir();
        Log.e("outPath===",outCachePath+"");
        File outFilePath = getExternalFilesDir(Environment.DIRECTORY_ALARMS);

        try {
            String outCacheSize = FileCacheUtils.getCacheSize(outCachePath);
            String outFileSize = FileCacheUtils.getCacheSize(outFilePath);

            Log.e("outPathCa===",outCacheSize+"");
            Log.e("outFileSize===",outFileSize+"");
            mTvCacheSize.setText(outCacheSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // TODO 暂时默认恢复缓存，后期修改
    private void setDefaultSize(){
        mTvCacheSize.setText("77.17k");
    }

}
